home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / win_m_p / mews11.zip / BUFFER.C < prev    next >
C/C++ Source or Header  |  1992-03-08  |  14KB  |  595 lines

  1. /*  BUFFER.C:    buffer mgmt. routines
  2.         MicroEMACS 3.10
  3.  
  4.  * Buffer management.
  5.  * Some of the functions are internal,
  6.  * and some are actually attached to user
  7.  * keys. Like everyone else, they set hints
  8.  * for the display system.
  9.  */
  10. #include    <stdio.h>
  11. #include    "estruct.h"
  12. #include    "eproto.h"
  13. #include    "edef.h"
  14. #include    "elang.h"
  15.  
  16. /*
  17.  * Attach a buffer to a window. The
  18.  * values of dot and mark come from the buffer
  19.  * if the use count is 0. Otherwise, they come
  20.  * from some other window.
  21.  */
  22. PASCAL NEAR usebuffer(f, n)
  23.  
  24. int f,n;    /* prefix flag and argument */
  25.  
  26. {
  27.     register BUFFER *bp;    /* temporary buffer pointer */
  28.  
  29.     /* get the buffer name to switch to */
  30.     bp = getdefb();
  31.     bp = getcbuf(TEXT24, bp ? bp->b_bname : mainbuf, TRUE);
  32. /*                "Use buffer" */
  33.     if (!bp)
  34.         return(ABORT);
  35.  
  36.     /* make it invisible if there is an argument */
  37.     if (f == TRUE)
  38.         bp->b_flag |= BFINVS;
  39.  
  40.     /* switch to it in any case */
  41.     return(swbuffer(bp));
  42. }
  43.  
  44. PASCAL NEAR nextbuffer(f, n)    /* switch to the next buffer in the buffer list */
  45.  
  46. int f, n;    /* default flag, numeric argument */
  47. {
  48.     register BUFFER *bp;    /* current eligable buffer */
  49.     register int status;
  50.  
  51.     /* make sure the arg is legit */
  52.     if (f == FALSE)
  53.         n = 1;
  54.     if (n < 1)
  55.         return(FALSE);
  56.  
  57.     /* cycle thru buffers until n runs out */
  58.     while (n-- > 0) {
  59.         bp = getdefb();
  60.         if (bp == NULL)
  61.             return(FALSE);
  62.         status = swbuffer(bp);
  63.         if (status != TRUE)
  64.             return(status);
  65.     }
  66.     return(status);
  67. }
  68.  
  69. PASCAL NEAR swbuffer(bp)    /* make buffer BP current */
  70.  
  71. BUFFER *bp;
  72.  
  73. {
  74.     register WINDOW *wp;
  75.     SCREEN *scrp;        /* screen to fix pointers in */
  76.     register int cmark;        /* current mark */
  77.  
  78.     /* let a user macro get hold of things...if he wants */
  79.     execkey(&exbhook, FALSE, 1);
  80.  
  81.     if (--curbp->b_nwnd == 0) {        /* Last use.        */
  82.         curbp->b_dotp  = curwp->w_dotp;
  83.         curbp->b_doto  = curwp->w_doto;
  84.         for (cmark = 0; cmark < NMARKS; cmark++) {
  85.             curbp->b_markp[cmark] = curwp->w_markp[cmark];
  86.             curbp->b_marko[cmark] = curwp->w_marko[cmark];
  87.         }
  88.         curbp->b_fcol  = curwp->w_fcol;
  89.     }
  90.     curbp = bp;                /* Switch.        */
  91.     if (curbp->b_active != TRUE) {        /* buffer not active yet*/
  92.         /* read it in and activate it */
  93.         readin(curbp->b_fname, ((curbp->b_mode&MDVIEW) == 0));
  94.         curbp->b_dotp = lforw(curbp->b_linep);
  95.         curbp->b_doto = 0;
  96.         curbp->b_active = TRUE;
  97.     }
  98.     curwp->w_bufp  = bp;
  99.     curwp->w_linep = bp->b_linep;        /* For macros, ignored. */
  100.     curwp->w_flag |= WFMODE|WFFORCE|WFHARD; /* Quite nasty.     */
  101.     if (bp->b_nwnd++ == 0) {        /* First use.        */
  102.         curwp->w_dotp  = bp->b_dotp;
  103.         curwp->w_doto  = bp->b_doto;
  104.         for (cmark = 0; cmark < NMARKS; cmark++) {
  105.             curwp->w_markp[cmark] = bp->b_markp[cmark];
  106.             curwp->w_marko[cmark] = bp->b_marko[cmark];
  107.         }
  108.         curwp->w_fcol  = bp->b_fcol;
  109.     } else {
  110.         /* in all screens.... */
  111.         scrp = first_screen;
  112.         while (scrp) {
  113.             wp = scrp->s_first_window;
  114.             while (wp != NULL) {
  115.                 if (wp!=curwp && wp->w_bufp==bp) {
  116.                     curwp->w_dotp  = wp->w_dotp;
  117.                     curwp->w_doto  = wp->w_doto;
  118.                     for (cmark = 0; cmark < NMARKS; cmark++) {
  119.                         curwp->w_markp[cmark] = wp->w_markp[cmark];
  120.                         curwp->w_marko[cmark] = wp->w_marko[cmark];
  121.                     }
  122.                     curwp->w_fcol  = wp->w_fcol;
  123.                     break;
  124.                 }
  125.                 /* next window */
  126.                 wp = wp->w_wndp;
  127.             }
  128.  
  129.             /* next screen! */
  130.             scrp = scrp->s_next_screen;
  131.         }
  132.     }
  133.  
  134.     /* let a user macro get hold of things...if he wants */
  135.     execkey(&bufhook, FALSE, 1);
  136.  
  137.     return(TRUE);
  138. }
  139.  
  140. /*
  141.  * Dispose of a buffer, by name.
  142.  * Ask for the name. Look it up (don't get too
  143.  * upset if it isn't there at all!). Get quite upset
  144.  * if the buffer is being displayed. Clear the buffer (ask
  145.  * if the buffer has been changed). Then free the header
  146.  * line and the buffer header. Bound to "C-X K".
  147.  */
  148. PASCAL NEAR killbuffer(f, n)
  149.  
  150. int f,n;    /* prefix flag and argument */
  151.  
  152. {
  153.     register BUFFER *bp;    /* ptr to buffer to dump */
  154.  
  155.     /* get the buffer name to kill */
  156.     bp = getdefb();
  157.     bp = getcbuf(TEXT26, bp ? bp->b_bname : mainbuf, TRUE);
  158. /*             "Kill buffer" */
  159.     if (bp == NULL)
  160.         return(ABORT);
  161.  
  162.     return(zotbuf(bp));
  163. }
  164.  
  165. /*    Allow the user to pop up a buffer, like we do.... */
  166.  
  167. PASCAL NEAR popbuffer(f, n)
  168.  
  169. int f, n;    /* default and numeric arguments */
  170.  
  171. {
  172.     register BUFFER *bp;    /* ptr to buffer to dump */
  173.  
  174.     /* get the buffer name to pop */
  175.     bp = getdefb();
  176.     bp = getcbuf(TEXT27, bp ? bp->b_bname : mainbuf, TRUE);
  177. /*             "Pop buffer" */
  178.     if (bp == NULL)
  179.         return(ABORT);
  180.  
  181.     /* make it invisible if there is an argument */
  182.     if (f == TRUE)
  183.         bp->b_flag |= BFINVS;
  184.  
  185.     return(pop(bp));
  186. }
  187.  
  188. BUFFER *PASCAL NEAR getdefb()    /* get the default buffer for a use or kill */
  189.  
  190. {
  191.     BUFFER *bp;    /* default buffer */
  192.  
  193.     /* Find the next buffer, which will be the default */
  194.     bp = curbp->b_bufp;
  195.  
  196.     /* cycle through the buffers to find an eligable one */
  197.     while (bp == NULL || bp->b_flag & BFINVS) {
  198.         if (bp == NULL)
  199.             bp = bheadp;
  200.         else
  201.             bp = bp->b_bufp;
  202.  
  203.         /* don't get caught in an infinite loop! */
  204.         if (bp == curbp) {
  205.             bp = NULL;
  206.             break;
  207.         }
  208.     }            
  209.     return(bp);
  210. }
  211.  
  212. PASCAL NEAR zotbuf(bp)    /* kill the buffer pointed to by bp */
  213.  
  214. register BUFFER *bp;
  215.  
  216. {
  217.     register BUFFER *bp1;
  218.     register BUFFER *bp2;
  219.     register int    s;
  220.  
  221.     /* we can not kill a displayed buffer */
  222.     if (bp->b_nwnd != 0) {
  223.         mlwrite(TEXT28);
  224. /*            "Buffer is being displayed" */
  225.         return(FALSE);
  226.     }
  227.  
  228.     /* we can not kill an executing buffer */
  229.     if (bp->b_exec != 0) {
  230.         mlwrite(TEXT226);
  231. /*            "%%Can not delete an executing buffer" */
  232.         return(FALSE);
  233.     }
  234.  
  235.     if ((s=bclear(bp)) != TRUE)        /* Blow text away.    */
  236.         return(s);
  237.     free((char *) bp->b_linep);        /* Release header line. */
  238.     bp1 = NULL;                /* Find the header.    */
  239.     bp2 = bheadp;
  240.     while (bp2 != bp) {
  241.         bp1 = bp2;
  242.         bp2 = bp2->b_bufp;
  243.     }
  244.     bp2 = bp2->b_bufp;            /* Next one in chain.    */
  245.     if (bp1 == NULL)            /* Unlink it.        */
  246.         bheadp = bp2;
  247.     else
  248.         bp1->b_bufp = bp2;
  249.     free((char *) bp);            /* Release buffer block */
  250.     return(TRUE);
  251. }
  252.  
  253. PASCAL NEAR namebuffer(f,n)    /*    Rename the current buffer    */
  254.  
  255. int f, n;        /* default Flag & Numeric arg */
  256.  
  257. {
  258.     register BUFFER *bp;    /* pointer to scan through all buffers */
  259.     char bufn[NBUFN];    /* buffer to hold buffer name */
  260.  
  261.     /* prompt for and get the new buffer name */
  262. ask:    if (mlreply(TEXT29, bufn, NBUFN) != TRUE)
  263. /*            "Change buffer name to: " */
  264.         return(FALSE);
  265.  
  266.     /* and check for duplicates */
  267.     bp = bheadp;
  268.     while (bp != NULL) {
  269.         if (bp != curbp) {
  270.             /* if the names the same */
  271.             if (strcmp(bufn, bp->b_bname) == 0)
  272.                 goto ask;  /* try again */
  273.         }
  274.         bp = bp->b_bufp;    /* onward */
  275.     }
  276.  
  277.     strcpy(curbp->b_bname, bufn);    /* copy buffer name to structure */
  278.     upmode();            /* make all mode lines replot */
  279.     mlerase();
  280.     return(TRUE);
  281. }
  282.  
  283. /*    Build and popup a buffer containing the list of all buffers.
  284.     Bound to "C-X C-B". A numeric argument forces it to list
  285.     invisible buffers as well.
  286. */
  287.  
  288. PASCAL NEAR listbuffers(f, n)
  289.  
  290. int f,n;    /* prefix flag and argument */
  291.  
  292. {
  293.     register int status;    /* stutus return */
  294.  
  295.     if ((status = makelist(f)) != TRUE)
  296.         return(status);
  297.     return(wpopup(blistp));
  298. }
  299.  
  300. /*
  301.  * This routine rebuilds the
  302.  * text in the special secret buffer
  303.  * that holds the buffer list. It is called
  304.  * by the list buffers command. Return TRUE
  305.  * if everything works. Return FALSE if there
  306.  * is an error (if there is no memory). Iflag
  307.  * indecates weather to list hidden buffers.
  308.  */
  309. PASCAL NEAR makelist(iflag)
  310.  
  311. int iflag;    /* list hidden buffer flag */
  312.  
  313. {
  314.     register char    *cp1;
  315.     register char    *cp2;
  316.     register int    c;
  317.     register BUFFER *bp;
  318.     register LINE    *lp;
  319.     register int    s;
  320.     register int    i;
  321.     long nbytes;        /* # of bytes in current buffer */
  322.     char b[7+1];
  323.     char line[128];
  324.  
  325.     blistp->b_flag &= ~BFCHG;        /* Don't complain!    */
  326.     if ((s=bclear(blistp)) != TRUE)     /* Blow old text away    */
  327.         return(s);
  328.     strcpy(blistp->b_fname, "");
  329.     if (addline(blistp, TEXT30) == FALSE
  330. /*            "ACT   Modes      Size Buffer       File" */
  331.     ||  addline(blistp, "--- --------- ------- --------------- ----") == FALSE)
  332.         return(FALSE);
  333.     bp = bheadp;                /* For all buffers    */
  334.  
  335.     /* build line to report global mode settings */
  336.     cp1 = &line[0];
  337.     *cp1++ = ' ';
  338.     *cp1++ = ' ';
  339.     *cp1++ = ' ';
  340.     *cp1++ = ' ';
  341.  
  342.     /* output the mode codes */
  343.     for (i = 0; i < NUMMODES; i++)
  344.         if (gmode & (1 << i))
  345.             *cp1++ = modecode[i];
  346.         else
  347.             *cp1++ = '.';
  348.     strcpy(cp1, TEXT31);
  349. /*            "          Global Modes" */
  350.     if (addline(blistp, line) == FALSE)
  351.         return(FALSE);
  352.  
  353.     /* output the list of buffers */
  354.     while (bp != NULL) {
  355.         /* skip invisible buffers if iflag is false */
  356.         if (((bp->b_flag&BFINVS) != 0) && (iflag != TRUE)) {
  357.             bp = bp->b_bufp;
  358.             continue;
  359.         }
  360.         cp1 = &line[0];         /* Start at left edge    */
  361.  
  362.         /* output status of ACTIVE flag (has the file been read in? */
  363.         if (bp->b_active == TRUE)    /* "@" if activated       */
  364.             *cp1++ = '@';
  365.         else
  366.             *cp1++ = ' ';
  367.  
  368.         /* output status of changed flag */
  369.         if ((bp->b_flag&BFCHG) != 0)    /* "*" if changed    */
  370.             *cp1++ = '*';
  371.         else
  372.             *cp1++ = ' ';
  373.  
  374.         /* report if the file is truncated */
  375.         if ((bp->b_flag&BFTRUNC) != 0)
  376.             *cp1++ = '#';
  377.         else
  378.             *cp1++ = ' ';
  379.  
  380.         *cp1++ = ' ';    /* space */
  381.  
  382.         /* output the mode codes */
  383.         for (i = 0; i < NUMMODES; i++) {
  384.             if (bp->b_mode & (1 << i))
  385.                 *cp1++ = modecode[i];
  386.             else
  387.                 *cp1++ = '.';
  388.         }
  389.         *cp1++ = ' ';            /* Gap.         */
  390.         nbytes = 0L;            /* Count bytes in buf.    */
  391.         lp = lforw(bp->b_linep);
  392.         while (lp != bp->b_linep) {
  393.             nbytes += (long)llength(lp)+1L;
  394.             lp = lforw(lp);
  395.         }
  396.         long_asc(b, 7, nbytes);         /* 6 digit buffer size. */
  397.         cp2 = &b[0];
  398.         while (*cp2)
  399.             *cp1++ = *cp2++;
  400.         *cp1++ = ' ';            /* Gap.         */
  401.         cp2 = &bp->b_bname[0];        /* Buffer name        */
  402.         while (*cp2)
  403.             *cp1++ = *cp2++;
  404.         *cp1++ = ' ';            /* Gap.         */
  405.         cp2 = &bp->b_fname[0];        /* File name        */
  406.         if (*cp2 != 0) {
  407.             while (cp1 < &line[38])
  408.                 *cp1++ = ' ';
  409.             while (*cp2)
  410.                 *cp1++ = *cp2++;
  411.         }
  412.         *cp1 = 0;          /* Add to the buffer.   */
  413.         if (addline(blistp, line) == FALSE)
  414.             return(FALSE);
  415.         bp = bp->b_bufp;
  416.     }
  417.     return(TRUE);                   /* All done           */
  418. }
  419.  
  420. /* Translate a long to ascii form. Don't trust various systems
  421.    ltoa() routines.. they aren't consistant                */
  422.  
  423. PASCAL NEAR long_asc(buf, width, num)
  424.  
  425. char   buf[];
  426. int    width;
  427. long   num;
  428.  
  429. {
  430.     buf[width] = 0;             /* End of string.    */
  431.     while (num >= 10) {            /* Conditional digits.    */
  432.         buf[--width] = (int)(num%10L) + '0';
  433.         num /= 10L;
  434.     }
  435.     buf[--width] = (int)num + '0';        /* Always 1 digit.    */
  436.     while (width != 0)            /* Pad with blanks.    */
  437.         buf[--width] = ' ';
  438. }
  439.  
  440. /*
  441.  * Look through the list of
  442.  * buffers. Return TRUE if there
  443.  * are any changed buffers. Buffers
  444.  * that hold magic internal stuff are
  445.  * not considered; who cares if the
  446.  * list of buffer names is hacked.
  447.  * Return FALSE if no buffers
  448.  * have been changed.
  449.  */
  450. PASCAL NEAR anycb()
  451. {
  452.     register BUFFER *bp;
  453.  
  454.     bp = bheadp;
  455.     while (bp != NULL) {
  456.         if ((bp->b_flag&BFINVS)==0 && (bp->b_flag&BFCHG)!=0)
  457.             return(TRUE);
  458.         bp = bp->b_bufp;
  459.     }
  460.     return(FALSE);
  461. }
  462.  
  463. /*
  464.  * Find a buffer, by name. Return a pointer
  465.  * to the BUFFER structure associated with it.
  466.  * If the buffer is not found
  467.  * and the "cflag" is TRUE, create it. The "bflag" is
  468.  * the settings for the flags in in buffer.
  469.  */
  470. BUFFER *PASCAL NEAR bfind(bname, cflag, bflag)
  471.  
  472. register char    *bname; /* name of buffer to find */
  473. int cflag;        /* create it if not found? */
  474. int bflag;        /* bit settings for a new buffer */
  475.  
  476. {
  477.     register BUFFER *bp;
  478.     register BUFFER *sb;    /* buffer to insert after */
  479.     register LINE    *lp;
  480.     int cmark;        /* current mark */
  481.  
  482.     bp = bheadp;
  483.     while (bp != NULL) {
  484.         if (strcmp(bname, bp->b_bname) == 0)
  485.             return(bp);
  486.         bp = bp->b_bufp;
  487.     }
  488.  
  489.     /* no such buffer exists, create it? */
  490.     if (cflag != FALSE) {
  491.  
  492.         /* allocate the needed memory */
  493.         if ((bp=(BUFFER *)malloc(sizeof(BUFFER))) == NULL)
  494.             return(NULL);
  495.         if ((lp=lalloc(0)) == NULL) {
  496.             free((char *) bp);
  497.             return(NULL);
  498.         }
  499.  
  500.         /* find the place in the list to insert this buffer */
  501.         if (bheadp == NULL || strcmp(bheadp->b_bname, bname) > 0) {
  502.             /* insert at the beginning */
  503.             bp->b_bufp = bheadp;
  504.             bheadp = bp;
  505.         } else {
  506.             sb = bheadp;
  507.             while (sb->b_bufp != NULL) {
  508.                 if (strcmp(sb->b_bufp->b_bname, bname) > 0)
  509.                     break;
  510.                 sb = sb->b_bufp;
  511.             }
  512.  
  513.             /* and insert it */
  514.             bp->b_bufp = sb->b_bufp;
  515.             sb->b_bufp = bp;
  516.         }
  517.  
  518.         /* and set up the other buffer fields */
  519.         bp->b_topline = NULL;
  520.         bp->b_botline = NULL;
  521.         bp->b_active = TRUE;
  522.         bp->b_dotp  = lp;
  523.         bp->b_doto  = 0;
  524.         for (cmark = 0; cmark < NMARKS; cmark++) {
  525.             bp->b_markp[cmark] = NULL;
  526.             bp->b_marko[cmark] = 0;
  527.         }
  528.         bp->b_fcol  = 0;
  529.         bp->b_flag  = bflag;
  530.         bp->b_mode  = gmode;
  531.         bp->b_nwnd  = 0;
  532.         bp->b_exec  = 0;
  533.         bp->b_linep = lp;
  534.         strcpy(bp->b_fname, "");
  535.         strcpy(bp->b_bname, bname);
  536. #if    CRYPT
  537.         bp->b_key[0] = 0;
  538. #endif
  539.         lp->l_fp = lp;
  540.         lp->l_bp = lp;
  541.     }
  542.     return(bp);
  543. }
  544.  
  545. /*
  546.  * This routine blows away all of the text
  547.  * in a buffer. If the buffer is marked as changed
  548.  * then we ask if it is ok to blow it away; this is
  549.  * to save the user the grief of losing text. The
  550.  * window chain is nearly always wrong if this gets
  551.  * called; the caller must arrange for the updates
  552.  * that are required. Return TRUE if everything
  553.  * looks good.
  554.  */
  555. PASCAL NEAR bclear(bp)
  556. register BUFFER *bp;
  557. {
  558.     register LINE    *lp;
  559.     register int    s;
  560.     int cmark;        /* current mark */
  561.  
  562.     if ((bp->b_flag&BFINVS) == 0        /* Not scratch buffer.    */
  563.     && (bp->b_flag&BFCHG) != 0        /* Something changed    */
  564.     && (s=mlyesno(TEXT32)) != TRUE)
  565. /*              "Discard changes" */
  566.         return(s);
  567.     bp->b_flag  &= ~BFCHG;            /* Not changed        */
  568.     while ((lp=lforw(bp->b_linep)) != bp->b_linep)
  569.         lfree(lp);
  570.     bp->b_dotp  = bp->b_linep;        /* Fix "."        */
  571.     bp->b_doto  = 0;
  572.     for (cmark = 0; cmark < NMARKS; cmark++) {
  573.         bp->b_markp[cmark] = NULL;  /* Invalidate "mark"    */
  574.         bp->b_marko[cmark] = 0;
  575.     }
  576.     bp->b_fcol = 0;
  577.     return(TRUE);
  578. }
  579.  
  580. PASCAL NEAR unmark(f, n)    /* unmark the current buffers change flag */
  581.  
  582. int f, n;    /* unused command arguments */
  583.  
  584. {
  585.     register WINDOW *wp;
  586.  
  587.     /* unmark the buffer */
  588.     curbp->b_flag &= ~BFCHG;
  589.  
  590.     /* unmark all windows as well */
  591.     upmode();
  592.  
  593.     return(TRUE);
  594. }
  595.